package com.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.KnowledgeTypeRelationVo;
import com.zb.entity.ZjProduct;
import com.zb.entity.ZjProductKnowledgeType;
import com.zb.extend.ProductKnowledgeTypeExtend;
import com.zb.mapper.ZjProductKnowledgeTypeMapper;
import com.zb.service.ZjProductKnowledgeTypeService;
import com.zb.service.ZjProductService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 产品知识库分类关联表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-11-13
 */
@Service
public class ZjProductKnowledgeTypeServiceImpl extends ServiceImpl<ZjProductKnowledgeTypeMapper, ZjProductKnowledgeType> implements ZjProductKnowledgeTypeService {
    @Resource
    private ZjProductService productService;

    // 新增、修改、删除
    @Override
    public void save(Long custAccId, Long productId, List<KnowledgeTypeRelationVo> knowledgeTypeVos, List<Long> ids) {
        if (CollectionUtil.isNotEmpty(ids)) {
            this.removeByIds(ids);
        }
        if (CollectionUtil.isEmpty(knowledgeTypeVos)) {
            return;
        }
        List<ZjProductKnowledgeType> adds = new ArrayList<>();
        List<ZjProductKnowledgeType> updates = new ArrayList<>();
        ZjProductKnowledgeType type = null;
        for (KnowledgeTypeRelationVo vo : knowledgeTypeVos) {
            type = new ZjProductKnowledgeType();
            BeanUtil.copyProperties(vo, type);
            if (null != type.getId()) {
                updates.add(type);
                continue;
            }
            type.setId(IdWorker.getId());
            type.setCustAccId(custAccId);
            type.setProductId(productId);
            adds.add(type);
        }
        if (CollectionUtil.isNotEmpty(adds)) this.saveBatch(adds);
        if (CollectionUtil.isNotEmpty(updates)) this.updateBatchById(updates);
    }

    // 根据产品ID删除
    @Override
    public void deleteBatchByProductId(List<Long> productIds) {
        if (CollectionUtil.isEmpty(productIds)) {
            return;
        }
        QueryWrapper<ZjProductKnowledgeType> wrapper = new QueryWrapper<>();
        wrapper.in("product_id", productIds);
        this.remove(wrapper);
    }

    // 根据知识仓库分类ID获取关联产品信息
    @Override
    public List<ProductKnowledgeTypeExtend> getByKnowledgeTypeIds(List<Long> knowledgeTypeIds) {
        if (CollectionUtil.isEmpty(knowledgeTypeIds)) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjProductKnowledgeType> wrapper = new QueryWrapper<>();
        wrapper.select("id", "product_id", "knowledge_type_id");
        wrapper.in("knowledge_type_id", knowledgeTypeIds);
        List<ZjProductKnowledgeType> typeList = this.list(wrapper);
        if (CollectionUtil.isEmpty(typeList)) {
            return new ArrayList<>();
        }

        List<Long> productIds = typeList.stream().map(ZjProductKnowledgeType::getProductId).distinct().toList();
        QueryWrapper<ZjProduct> productQueryWrapper = new QueryWrapper<>();
        productQueryWrapper.select("id", "name", "model");
        productQueryWrapper.in("id", productIds);
        List<ZjProduct> productList = productService.list(productQueryWrapper);

        return typeList.stream().map(type -> {
            ProductKnowledgeTypeExtend extend = new ProductKnowledgeTypeExtend();
            BeanUtil.copyProperties(type, extend);
            ZjProduct product = productList.stream().filter(f -> Objects.equals(f.getId(), type.getProductId())).findFirst().orElse(null);
            if (null != product) {
                extend.setName(product.getName());
                extend.setModel(product.getModel());
            }
            return extend;
        }).toList();
    }
}
